package org.dstart.draw.cui.symbol.basic.iteam
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import org.dstart.draw.cerror.CParameterError;
	import org.dstart.draw.csuper.CSprite;
	import org.dstart.draw.cui.symbol.events.CWinTitleEvent;

	public class BWinTitleButton extends CSprite
	{
		private static var btnWidth:Number = 20;	//	按钮默认宽度
		private static var btnHeight:Number = 20;	//	按钮默认高度
		
		private var m_btnModel:Number;		//	按钮模式
		private var m_isCanUse:Boolean;		//	是否可以使用
		private var m_nowShow:Sprite;		//	当前显示出来的对象
		
		public function BWinTitleButton(mod:Number)
		{
			InitObject();
			
			this.model = mod;
		}
		
		//	设置按钮模式
		public function set model(num:Number):void
		{
			m_btnModel = num;
			
			DrawButtons();
		}
		
		//	设置按钮是否可用
		public function set enabled(flag:Boolean):void
		{
			this.m_isCanUse = flag;
			this.mouseChildren = this.mouseEnabled = flag;
			
			if(flag)
			{				
				this.sprNone.visible = false;	
				this.m_nowShow.visible = true;			
			}
			else
			{
				this.sprNone.visible = true;
				this.m_nowShow.visible = false;
			}
		}
		
		//	读取按钮是否可用标识
		public function get enabled():Boolean
		{
			return this.m_isCanUse;
		}
		
		//-------------------------------------------------------------
		
		private var sprHot:Sprite;		//	热响应区块
		private var sprNormal:Sprite;	//	正常状态
		private var sprROver:Sprite;	//	鼠标移上状态
		private var sprMDown:Sprite;	//	鼠标按下状态
		private var sprNone:Sprite;		//	不可用状态
		
		private function InitObject():void
		{
			sprHot = new Sprite();
			sprNormal = new Sprite();
			sprROver = new Sprite();
			sprMDown = new Sprite();
			sprNone = new Sprite();
			
			_root.addChild(sprHot);
			_root.addChild(sprNormal);
			_root.addChild(sprROver);
			_root.addChild(sprMDown);
			_root.addChild(sprNone);
		}		
		
		//	根据属性绘制不同的铵钮
		private function DrawButtons():void
		{
			if(this.m_btnModel)
			{
				switch(m_btnModel)
				{
					//	关闭按钮
					case BWinTitleButtonModel.CLOSE :
						DrawCloseButton();
						break;

					//	最小化按钮
					case BWinTitleButtonModel.MINIMIZE :
						DrawMinimizeButton();
						break;

					//	还原按钮
					case BWinTitleButtonModel.RESTORE :
						DrawRestoreButton();
						break;

					//	最大化按钮
					case BWinTitleButtonModel.MAXIMIZE :
						DrawMaximizeButton();
						break;

					//	帮助按钮
					case BWinTitleButtonModel.HELP :
						DrawHelpButton();
						break;
					
					default :
						throw new CParameterError("Model", "BWinTitleButton");
						break;
				}				
			}
		}
		
		//	绘制 关闭按钮
		private function DrawCloseButton():void
		{
			DrawHotSprite();
			DrawCloseSprite(sprNormal, 0x000000);
			DrawCloseSprite(sprROver, 0xFF0000);
			DrawCloseSprite(sprMDown, 0x999999);
			DrawCloseSprite(sprNone, 0xCCCCCC);			
		}
		
		//	绘制 最小化按钮
		private function DrawMinimizeButton():void
		{
			DrawHotSprite();
			DrawMinimizeSprite(sprNormal, 0x000000);
			DrawMinimizeSprite(sprROver, 0xFF0000);
			DrawMinimizeSprite(sprMDown, 0x999999);
			DrawMinimizeSprite(sprNone, 0xCCCCCC);			
		}

		//	绘制 最大化按钮
		private function DrawMaximizeButton():void
		{
			DrawHotSprite();
			DrawMaximizeSprite(sprNormal, 0x000000);
			DrawMaximizeSprite(sprROver, 0xFF0000);
			DrawMaximizeSprite(sprMDown, 0x999999);
			DrawMaximizeSprite(sprNone, 0xCCCCCC);			
		}

		//	绘制还原按钮
		private function DrawRestoreButton():void
		{
			DrawHotSprite();
			DrawRestoreSprite(sprNormal, 0x000000);
			DrawRestoreSprite(sprROver, 0xFF0000);
			DrawRestoreSprite(sprMDown, 0x999999);
			DrawRestoreSprite(sprNone, 0xCCCCCC);			
		}

		//	绘制关闭按钮
		private function DrawHelpButton():void
		{
			DrawHotSprite();
			DrawHelpSprite(sprNormal, 0x000000);
			DrawHelpSprite(sprROver, 0xFF0000);
			DrawHelpSprite(sprMDown, 0x999999);
			DrawHelpSprite(sprNone, 0xCCCCCC);			
		}
		
		//-------------------------------------------------------------------
		
		//	绘制背景透明色块
		private function DrawHotSprite():void
		{
			sprHot.graphics.clear();
			sprHot.graphics.beginFill(0xFF0000, 0);
			sprHot.graphics.drawRect(0, 0, btnWidth, btnHeight);
			sprHot.graphics.endFill();
		}
		
		//	绘制 关闭按钮状态
		private function DrawCloseSprite(spr:Sprite, col:uint):void
		{
			spr.graphics.clear()			
			spr.graphics.lineStyle(2, col, 1);
			spr.graphics.moveTo(4, 4);
			spr.graphics.lineTo(btnWidth - 4, btnHeight - 4);
			spr.graphics.moveTo(btnWidth - 4, 4);
			spr.graphics.lineTo(4, btnHeight - 4);			
		}
		
		//	绘制 最小化按钮
		private function DrawMinimizeSprite(spr:Sprite, col:uint):void
		{
			spr.graphics.clear();
			spr.graphics.lineStyle(3, col, 1);
			spr.graphics.moveTo(4, btnHeight - 5);
			spr.graphics.lineTo(btnWidth - 4, btnHeight - 5);
		}
		
		//	绘制 最大化按钮
		private function DrawMaximizeSprite(spr:Sprite, col:uint):void
		{
			spr.graphics.clear();
			spr.graphics.lineStyle(2, col, 1);
			spr.graphics.drawRect(4, 5, btnWidth - 7, btnHeight - 9);
			spr.graphics.moveTo(4, 4);
			spr.graphics.lineTo(btnWidth - 3, 4);
		}
		
		//	绘制 还原按钮
		private function DrawRestoreSprite(spr:Sprite, col:uint):void
		{
			spr.graphics.clear();
			spr.graphics.lineStyle(2, col, 1);
			spr.graphics.drawRect(4, 4, btnWidth - 7, btnHeight - 8);			
			// spr.graphics.lineStyle(1, col, 1);
			spr.graphics.drawRect(7, 7, 7, 6);			
		}

		//	绘制 帮助按钮
		private function DrawHelpSprite(spr:Sprite, col:uint):void
		{
			spr.graphics.clear();
			spr.graphics.lineStyle(3, col, 1);
			spr.graphics.moveTo(btnWidth/2 - 1, 3);
			spr.graphics.lineTo(btnHeight - 8, 3);
			spr.graphics.drawRect(btnWidth/2 - 3, btnHeight - 6, 3, 3);
		}
		
		//-----------------------------------------------------------------
		
		//	对象显示出来以后，设置事件侦听
		protected override function InitDraw():void
		{
			for(var i:int = 0; i < _root.numChildren; i++)
			{
				var spr:Sprite = _root.getChildAt(i) as Sprite;
				
				spr.mouseChildren = spr.mouseEnabled = false;
				spr.visible = false;				
			}
			
			sprNormal.visible = true;
			sprHot.visible = true;
			
			this.addEventListener(MouseEvent.ROLL_OVER, onRollOverEvent);
			this.addEventListener(MouseEvent.ROLL_OUT, onRollOutEvent);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownEvent);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseUpEvent);			
		}
		
		private var isDownFlag:Boolean;		//	是否鼠标按下了

		//	鼠标移上时事件
		private function onRollOverEvent(e:MouseEvent):void
		{
			if(isDownFlag)
			{
				RushShow(this.sprMDown);
			}
			else
			{
				RushShow(this.sprROver);
			}
			
		}
		
		//	鼠标移开时事件
		private function onRollOutEvent(e:MouseEvent):void
		{
			RushShow(this.sprNormal);
		}
		
		//	鼠标按下事件
		private function onMouseDownEvent(e:MouseEvent):void
		{
			isDownFlag = true;			
			
			RushShow(this.sprMDown);
			
			this.stage.addEventListener(MouseEvent.MOUSE_UP, onStageUpEvent);
		}
		
		//	正常状态下鼠标松开事件
		private function onMouseUpEvent(e:MouseEvent):void
		{
			isDownFlag = false;
			
			RushShow(this.sprROver);
			SendEvel();
			
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, onStageUpEvent);
		}
		
		//	鼠标在按钮外部松开事件
		private function onStageUpEvent(e:MouseEvent):void
		{
			isDownFlag = false;
			
			RushShow(this.sprNormal);
			SendEvel();
			
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, onStageUpEvent);
		}
		
		//	更新显示
		private function RushShow(spr:Sprite):void
		{
			this.sprNormal.visible = (spr.name == this.sprNormal.name) ? true : false;
			this.sprROver.visible = (spr.name == this.sprROver.name) ? true : false;
			this.sprMDown.visible = (spr.name == this.sprMDown.name) ? true : false;
			this.sprNone.visible = (spr.name == this.sprNone.name) ? true : false;
		}
		
		private function SendEvel():void
		{
			if(m_btnModel)
			{
				switch(m_btnModel)
				{
					//	关闭按钮
					case BWinTitleButtonModel.CLOSE :
						SendMessage(CWinTitleEvent.CLOSE);
						break;

					//	最小化按钮
					case BWinTitleButtonModel.MINIMIZE :						
						SendMessage(CWinTitleEvent.MINIMIZE);
						break;

					//	还原按钮
					case BWinTitleButtonModel.RESTORE :
						SendMessage(CWinTitleEvent.RESTORE);
						break;

					//	最大化按钮
					case BWinTitleButtonModel.MAXIMIZE :
						SendMessage(CWinTitleEvent.MAXIMIZE);
						break;

					//	帮助按钮
					case BWinTitleButtonModel.HELP :
						SendMessage(CWinTitleEvent.HELP);
						break;
				}
			}
		}
		
		private function SendMessage(str:String):void
		{
			this.dispatchEvent(new CWinTitleEvent(str));
		}

	}
}